﻿Imports pdflib
Imports System.IO
Public Class clsTestPDF
    Private _pdfPath As String
    Private _options As clsTestOptions
    Private _pdf As ClsPDF
    Private _pdfPages As List(Of clsPDFPage)
    Private _annotation_1 As ClsPdfAnnotation
    Private _annotation_2 As ClsPdfAnnotation
    Private _imagelist As List(Of clsPDFImage)

    Public Property Success As Boolean
    Public Property TestStage As String
    Public Property TestImageList As New List(Of clsPDFImage)
    Public Property SaveChangesToPdfFolder As String
    Public Property LastException As Exception
    Public Sub New(pdfPath As String, options As clsTestOptions)
        _pdfPath = pdfPath
        _options = options
        Success = True
        LastException = Nothing
    End Sub
    Public Sub PerformTest()
        Try
            TestStage = "Not a PDF file or PDF is damaged"
            _pdf = New ClsPDF(_pdfPath)

            If _options.BasicRead Then
                TestStage = "Basic Read Failed"
                _performBasicReadTest()
            End If

            If _options.GetAllPageInformation Then
                TestStage = "Getting All Page Information failed"
                _performGetAllPageInformationTest()
            End If

            If _options.CreateAnnotations Then
                TestStage = "Create Annotations failed"
                _performCreateAnnontationsTest()
            End If

            If _options.CreateImages Then
                TestStage = "Create Images failed"
                _performCreateImagesTest()
            End If

            If Not _pdf.IsEncrypt Then
                If _options.GetImages Then
                    TestStage = "Getting All Images failed"
                    _performGetImagesTest()
                End If

                If _options.ExtractImages Then
                    TestStage = "Extract Images failed"
                    _performExtractImagesTest()
                End If

                If _options.AddAnnotations Then
                    TestStage = "Add Annotations failed"
                    _performAddAnnotationsTest()
                End If

                If _options.AddImagesToPages Then
                    TestStage = "Add Images failed"
                    _performAddImagesToPagesTest()
                End If

                If _options.ErasePageContent Then
                    TestStage = "Erase Page Content failed"
                    _performErasePageContentTest()
                End If

                If _options.RotatePage Then
                    TestStage = "Rotate Page failed"
                    _performRotatePageTest()
                End If

                If _options.ChangeDocumentInformationAndViewPreferences Then
                    TestStage = "Change Document Information failed"
                    _performChangeDocumentInformationTest()
                    TestStage = "Change Viewer Preferences failed"
                    _performChangeViewerPreferencesTest()
                End If

                If _options.SaveChangesToPDF Then
                    TestStage = "Save Changes to PDF failed"
                    _performSavePdfChangesTest()
                End If
            End If

            TestStage = "Closing PDF file failed"
            _pdf.Close()
            TestStage = "Success"
            Success = True
            LastException = Nothing
        Catch ex As Exception
            If Not _pdf Is Nothing Then
                _pdf.Close()
            End If
            _pdf = Nothing
            Success = False
            LastException = ex
        Finally
        End Try
    End Sub
    Private Sub _performSavePdfChangesTest()
        _pdf.Save(SaveChangesToPdfFolder + "\" + _pdf.FileInfo.Name)
    End Sub
    Private Sub _performChangeDocumentInformationTest()
        _pdf.DocumentInformation.Keywords = "Some keywords"
        _pdf.DocumentInformation.Author = "A New Author"
        _pdf.DocumentInformation.Title = "A New Title"
        _pdf.DocumentInformation.Producer = "Unknown"
        _pdf.DocumentInformation.Subject = "A New Subject"
        _pdf.DocumentInformation.Creator = "PDFLib TestBed"
    End Sub
    Private Sub _performChangeViewerPreferencesTest()
        If _pdf.ViewerPreferences Is Nothing Then
            'If the document do not contain preferences, with create them
            _pdf.CreateViewerPreferences()
        End If

        'We add some viewer preferences
        _pdf.ViewerPreferences.HideWindowUI = True
        _pdf.ViewerPreferences.CenterWindow = True
        _pdf.ViewerPreferences.HideMenubar = True
        _pdf.ViewerPreferences.HideToolbar = True
        _pdf.ViewerPreferences.NumCopies = 5
        _pdf.ViewerPreferences.NonFullScreenPageMode = NonFullScreenPageModeTypes.UseThumbs
        _pdf.ViewerPreferences.Duplex = DuplexTypes.Simplex
        _pdf.ViewerPreferences.DisplayDocTitle = True
        _pdf.ViewerPreferences.PickTrayByPDFSize = True
        _pdf.ViewerPreferences.PrintScaling = PrintScalingTypes.AppDefault
        _pdf.ViewerPreferences.PageLayout = PageLayoutTypes.TwoPageLeft
        _pdf.ViewerPreferences.PageMode = PageModeTypes.UseThumbs
    End Sub
    Private Sub _performRotatePageTest()
        Dim page As clsPDFPage
        page = _pdf.GetPage(3)
        If Not page Is Nothing Then
            page.Rotation = pdflib.PageRotationTypes.Rotation_180
        End If
    End Sub
    Private Sub _performErasePageContentTest()
        Dim page As clsPDFPage
        page = _pdf.GetPage(2)
        If Not page Is Nothing Then
            page.EraseContent()
        End If
    End Sub
    Private Sub _performAddImagesToPagesTest()
        Dim page As clsPDFPage
        Dim im As clsPDFImage
        Dim posy As Long

        page = _pdf.GetPage(1)
        posy = 400
        If Not page Is Nothing Then
            For Each im In TestImageList
                page.AddImage(im, 250, posy, 50, 50)
                posy -= 75
            Next
        End If

        If _pdf.PageCount > 1 Then
            posy = 400
            page = _pdf.GetPage(_pdf.PageCount)
            For Each im In TestImageList
                page.AddImage(im, 250, posy, 50, 50)
                posy -= 75
            Next
        End If

    End Sub
    Private Sub _performCreateImagesTest()
        '_image_1 = New clsPDFImage(Application.StartupPath + "\JpegTestImages\" + "images.jpg")
        '_image_2 = New clsPDFImage(Application.StartupPath + "\JpegTestImages\" + "ios.jpg")
    End Sub

    Private Sub _performAddAnnotationsTest()
        Dim page As clsPDFPage

        For Each page In _pdfPages
            page.AddAnnotation(_annotation_1)
            page.AddAnnotation(_annotation_2)
        Next
    End Sub
    Private Sub _performCreateAnnontationsTest()
        _annotation_1 = New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
        _annotation_1.Contents = "This is an annotation"
        _annotation_1.Location(300, 300, 300, 300)

        _annotation_2 = New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
        _annotation_2.SetCharacteristic = pdflib.AnnotationFlags.Locked Or pdflib.AnnotationFlags.ReadOnlyFlag
        _annotation_2.Contents = "Another one (This one is locked!)"
        _annotation_2.Location(200, 200, 200, 200)

    End Sub
    Private Sub _performExtractImagesTest()
        Dim count As Integer
        Dim image As clsPDFImage

        If _imagelist.Count > 0 Then
            Dim dir As New DirectoryInfo(My.Settings.extractImagesFolder + "\" + _pdf.FileInfo.Name)
            If Not dir.Exists Then
                dir.Create()
            End If

            count = 0
            For Each image In _imagelist
                count += 1
                image.Save(dir.FullName + "\" + "image_" + count.ToString)
            Next
        End If

    End Sub
    Private Sub _performGetImagesTest()
        Dim nbrPage As Integer
        Dim page As clsPDFPage
        Dim liste As List(Of clsPDFImage)
        Dim image As clsPDFImage

        _imagelist = New List(Of clsPDFImage)
        nbrPage = 0
        For Each page In _pdf.GetPages
            If nbrPage = 10 Then
                Exit For
            End If

            liste = page.GetImages
            For Each image In liste
                _imagelist.Add(image)
            Next
            nbrPage += 1
        Next

        _imagelist = _pdf.GetImages
    End Sub
    Private Sub _performBasicReadTest()
        Dim version As String = _pdf.Version
        Dim pageCount As Integer = _pdf.PageCount
        Dim fastView As Boolean = _pdf.IsLinearized
        If _pdf.IsEncrypt Then
            Dim filter As String = _pdf.EncryptionInformation.Filter
            Dim canPrint As Boolean = _pdf.EncryptionInformation.UserRight(UserAccess.CanPrint)
        Else
            Dim title As String = _pdf.DocumentInformation.Title
            Dim author As String = _pdf.DocumentInformation.Author
            Dim subject As String = _pdf.DocumentInformation.Subject
            Dim keyword As String = _pdf.DocumentInformation.Keywords
            Dim creator As String = _pdf.DocumentInformation.Creator
            Dim producer As String = _pdf.DocumentInformation.Producer
            Dim creationDate As String = _pdf.DocumentInformation.CreationDate
            Dim modDate As String = _pdf.DocumentInformation.GetModificationDate
        End If
    End Sub
    Private Sub _performGetAllPageInformationTest()
        Dim page As clsPDFPage
        Dim i As Integer

        _pdfPages = New List(Of clsPDFPage)
        For i = 1 To _pdf.PageCount
            page = _pdf.GetPage(i)
            _pdfPages.Add(page)
        Next
    End Sub
End Class
Public Class clsTestOptions
    Public Property Options As TestOptions
    Sub New()

    End Sub
    Public Property BasicRead As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.BasicRead)
        End Set
        Get
            BasicRead = Options.HasFlag(TestOptions.BasicRead)
        End Get
    End Property
    Public Property GetAllPageInformation As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.GetAllPageInformation)
        End Set
        Get
            GetAllPageInformation = Options.HasFlag(TestOptions.GetAllPageInformation)
        End Get
    End Property
    Public Property GetImages As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.GetImages)
        End Set
        Get
            GetImages = Options.HasFlag(TestOptions.GetImages)
        End Get
    End Property
    Public Property ExtractImages As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.ExtractImages)
        End Set
        Get
            ExtractImages = Options.HasFlag(TestOptions.ExtractImages)
        End Get
    End Property
    Public Property CreateAnnotations As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.CreateAnnotations)
        End Set
        Get
            CreateAnnotations = Options.HasFlag(TestOptions.CreateAnnotations)
        End Get
    End Property
    Public Property AddAnnotations As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.AddAnnotations)
        End Set
        Get
            AddAnnotations = Options.HasFlag(TestOptions.AddAnnotations)
        End Get
    End Property
    Public Property CreateImages As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.CreateImages)
        End Set
        Get
            CreateImages = Options.HasFlag(TestOptions.CreateImages)
        End Get
    End Property
    Public Property AddImagesToPages As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.AddImagesToPages)
        End Set
        Get
            AddImagesToPages = Options.HasFlag(TestOptions.AddImagesToPages)
        End Get
    End Property
    Public Property ErasePageContent As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.ErasePageContent)
        End Set
        Get
            ErasePageContent = Options.HasFlag(TestOptions.ErasePageContent)
        End Get
    End Property
    Public Property RotatePage As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.RotatePage)
        End Set
        Get
            RotatePage = Options.HasFlag(TestOptions.RotatePage)
        End Get
    End Property
    Public Property ChangeDocumentInformationAndViewPreferences As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.ChangeDocumentInformation)
        End Set
        Get
            ChangeDocumentInformationAndViewPreferences = Options.HasFlag(TestOptions.ChangeDocumentInformation)
        End Get
    End Property
    Public Property SaveChangesToPDF As Boolean
        Set(value As Boolean)
            _setFlag(value, TestOptions.SaveChangesToPDF)
        End Set
        Get
            SaveChangesToPDF = Options.HasFlag(TestOptions.SaveChangesToPDF)
        End Get
    End Property
    Private Sub _setFlag(b As Boolean, flag As TestOptions)
        If b Then
            Options = Options Or flag
        Else
            Options = Options Xor flag
        End If
    End Sub
End Class
<Flags> Public Enum TestOptions
    BasicRead = 1
    GetAllPageInformation = 2
    GetImages = 4
    ExtractImages = 8
    CreateAnnotations = 16
    AddAnnotations = 32
    CreateImages = 64
    AddImagesToPages = 128
    ErasePageContent = 256
    RotatePage = 512
    ChangeDocumentInformation = 1024
    SaveChangesToPDF = 2048
End Enum